/*
 * Player.cpp
 *
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses 
 */

#include "Bomberman.h"
#include "MovingObject.h"
#include "ModelConstants.h"

Bomberman::Bomberman(int x, int y)
    : MovingObject(x, y) {
    init();
}

Bomberman::Bomberman(int controling_player_id, int x, int y)
    : MovingObject(x, y) {
    init();
    this->controling_player_id = controling_player_id;
}

void Bomberman::init() {
    Properties properties = Properties();
    resistance = properties.getPropertyAsInt("bomberman_resistance");
    full_resistance = resistance;
    speed = properties.getPropertyAsInt("bomberman_speed");
//    lifes = 1; //TODO DEBUG, lo real es la línea comentada de abajo
    lifes = properties.getPropertyAsInt("player_lifes");
    spawn_x = x;
    spawn_y = y;
    alignment = AL_PLAYER;
    controling_player_id = -1;
    drop_frequency = properties.getPropertyAsInt("bomberman_drop_frequency");
    tics_since_drop = drop_frequency + 1;
    representation = BOMBERMAN;
}

void Bomberman::setControlingPlayerId(int player_id) {
    controling_player_id = player_id;
}

int Bomberman::getControlingPlayerId() {
    return controling_player_id;
}

void Bomberman::move(Map& map) {
    tics_since_drop++;
    tics_since_moved++;
    if ((tics_since_moved >= speed) && (direction != NO_DIRECTION)) {
        if (nextMoveIsInvalid(map)) {
            return;
        }
        advance(map);
        notify(OBJECT_MOVED, this);
        tics_since_moved = 0;
        direction = NO_DIRECTION;
    }
}

void Bomberman::movingTo(int x, int y) {
    int dx = this->x - x;
    int dy = this->y - y;
    if (dy > 0) {
        direction = UP;
    } else if (dy < 0) {
        direction = DOWN;
    } else if (dx < 0) {
        direction = RIGHT;
    } else if (dx > 0) {
        direction = LEFT;
    }
}

TimedBomb* Bomberman::dropBomb() {
    if (! entered) {
        return 0;
    }
    if (tics_since_drop < drop_frequency) {
        return 0;
    }
    tics_since_drop = 0;
    return new TimedBomb(x, y, this);
}

bool Bomberman::getCollidedBy(MapObject* object) {
    return (object->getAlignment() != AL_PLAYER);
}

void Bomberman::receiveDamage(int damage, MapObject* from) {
    if (! entered) {
        return;
    }
    resistance -= damage;
    if (resistance <= 0) {
        notify(OBJECT_DESTROYED, this, from);
        entered = false;
        tics_since_drop = 0;
        direction = NO_DIRECTION;
        tics_since_moved = 0;
        lifes--;
    }
}

bool Bomberman::canRevive() {
    return lifes > 0;
}

void Bomberman::restoreResistance(int amount) {
    if (amount > 0) {
        resistance+= amount;
    } else if ((resistance > full_resistance) || (amount < 0)) {
        resistance = full_resistance;
    }
}

Bomberman* Bomberman::clone() {
    return new Bomberman(*this);
}

void Bomberman::setRepresentation(ModelObject representation) {
    this->representation = representation;
}

ModelObject Bomberman::getRepresentation() {
    return representation;
}
